//  Created by <Omar MALAGOUEN>

//  Copyright 2011 All rights reserved.
//

#include "log.h"


QTextStream *out =0;


void CreerLog()
{
#if defined(Q_OS_WIN32) || defined(Q_OS_WIN64)
    QString myHome = QString(getenv("USERPROFILE"));
#else
    QString myHome = QString(getenv("HOME"));
#endif

    QString appDir = myHome + "/.kakola";
    QDir appQDir(appDir);
    if ( ! appQDir.exists()){
        if ( appQDir.mkdir(appDir) == false )
            qDebug("directory %s doest not exist, error whil creating directory",appDir.toLatin1().data());
    }

    QString strLogDir = myHome + "/.kakola/log/";
    QDir logDir(strLogDir);
    if ( ! logDir.exists()){
        if ( logDir.mkdir(strLogDir) == false )
            qDebug("directory %s doest not exist, error whil creating directory",strLogDir.toLatin1().data());
    }

    QString filename = strLogDir + "/kakola"
                       + "-" + QDateTime::currentDateTime().toString(QLatin1String("yyyy.MM.dd-hh.mm.ss"))
                       + ".log";

    QSettings *settings = new QSettings(QSettings::NativeFormat,
                                        QSettings::UserScope,QCoreApplication::organizationName(),
                                        QCoreApplication::applicationName());
    settings->setValue("logfile",filename);
    QFile* fichierLog = new QFile(filename);
    if(fichierLog->open(QIODevice::WriteOnly))
    {
        out = new QTextStream(fichierLog);
        qInstallMsgHandler(InformationLog);
    }
}

void InformationLog(QtMsgType type, const char *message)
{
    QString date = QDateTime::currentDateTime().toString(QLatin1String("[yyyy.MM.dd-hh.mm.ss] " ));
    (*out) << date << message << endl;
}

void Information(char* message)
{
}
